package com.chalmers.foa.gamemodel.gameobjects.effects.impl;

import android.graphics.Point;

import com.chalmers.foa.gamehandlers.impl.CollisionHandler;
import com.chalmers.foa.gamehandlers.impl.PhysicsHandler;
import com.chalmers.foa.gamemodel.gameobjects.effects.ParticleAdapter;

public class Particle extends ParticleAdapter {
	
	private Point startVelocity;
	private Point velocity;
	private boolean alive;
	private int startLifeTime;
	private int lifeTime;
	
	public Particle(Point position, int color, int direction, int speed, int lifeTime, int size) {
		super(color, new Point(size,size),new Point(position));
		velocity = new Point();
		velocity.x = (int)((double)speed * Math.cos(Math.toRadians((double)direction)));
		velocity.y = (int)((double)speed * Math.sin(Math.toRadians((double)direction)));
		startVelocity = new Point(velocity);
		this.startLifeTime = lifeTime;
		this.lifeTime=lifeTime;
		alive = true;
		
		if(direction < 0)
			direction = 0;
		else if(direction > 360)
			direction = 360;
	}
	
	public void update() {
		lifeTime--;
		if(lifeTime <= 0)
			alive = false;
		move();
	}
	public void reset(){
		lifeTime = startLifeTime;
		velocity.set(startVelocity.x,startVelocity.y);
		alive=true;
	}
	@Override
	public void setPosition(Point position){
		super.setPosition(position);
		reset();
	}
	
	private void move() {
		PhysicsHandler.getInstance().applyGravitationToOffset(velocity, 1);
		if(CollisionHandler.getInstance().objectOutsideWindowYAxis(this, velocity))
			velocity.y = - velocity.y;
		if(CollisionHandler.getInstance().objectOutsideWindowXAxis(this, velocity))
			velocity.x = - velocity.x;
		translate(velocity);
	}
	
	public boolean isAlive() {
		return alive;
	}
}
